#include <QDebug>

#include "core/transactioncontroller.h"
#include "core/transactionmodel.h"
#include "gui/transactionview.h"
#include "gui/transactionviewdetailsdialog.h"
#include "gui/transactionviewadddialog.h"
#include "gui/transactionviewreportsdialog.h"
#include "gui/transactionvieweditdialog.h"

void TransactionView::onDataUpdate(QList<BaseModel *> models)
{
    TransactionModel *transaction;
    ShinyListItem *listItem;
    QDateTime saleTime;
    QString headerText = "Sale made on ";
    QString subText = "Sold to ";
    int i;

    qDebug() << "TransactionView received update";
    BaseView::onDataUpdate(models);

    slist->clear();
    for (i = 0; i < models.length(); i++) {
        transaction = (TransactionModel *) models.at(i);
        listItem = new ShinyListItem;
        listItem->setItemIcon("res/transaction.png");
        saleTime = transaction->getSaleTime();

        listItem->setMainText(headerText + saleTime.toString("ddd MMMM d, yyyy"));
        listItem->setSubText(subText + transaction->getCustomer()->getName());
        slist->addItem(listItem);
    }
}

void TransactionView::onDetailsRequested(BaseModel *model)
{
    int result;
    TransactionModel *transaction = (TransactionModel *) model;
    TransactionViewDetailsDialog *dialog = new TransactionViewDetailsDialog(this, transaction);
    result = dialog->exec();

    if (result == QDialog::Accepted) {
        TransactionViewEditDialog *editDialog = new TransactionViewEditDialog(this, transaction);
        editDialog->exec();
        delete editDialog;
    }

    delete dialog;

}

void TransactionView::onAddButtonClicked()
{
    TransactionModel *transaction;
    TransactionController *ctrl = (TransactionController *) this->getController();
    TransactionViewAddDialog *dialog = new TransactionViewAddDialog(this);
    dialog->exec();

    transaction = dialog->getTransaction();

    // If "Cancel" is pressed, getTransaction() will return NULL
    if (transaction) {
        ctrl->addTransaction(transaction);
        ctrl->getAll();
    }

    delete dialog;
}

void TransactionView::onReportsButtonClicked()
{
    TransactionViewReportsDialog *dialog = new TransactionViewReportsDialog(this);
    dialog->exec();
    delete dialog;
}

void TransactionView::initViewComponents()
{
    BaseView::initViewComponents();

    topLabel->setText("Double-click a Transaction to see more details.");
    addButton->setText("Add a Transaction");
}

void TransactionView::initViewSignalsAndSlots()
{
    BaseView::initViewSignalsAndSlots();

    connect(this, SIGNAL(detailsRequested(BaseModel*)),
            this, SLOT(onDetailsRequested(BaseModel*)));
    connect(this->addButton, SIGNAL(clicked()),
            this, SLOT(onAddButtonClicked()));
    connect(this->reportsButton, SIGNAL(clicked()),
            this, SLOT(onReportsButtonClicked()));
}

TransactionView::TransactionView(QWidget *parent) :
    BaseView(parent)
{
    this->initViewComponents();
    this->initViewSignalsAndSlots();
}
